import types from './types';
import { CHECKED } from "../../../constants/common";
import { _subTitle } from "../check-length";
import { isChecked } from '../../../utils/statusDetect';
const { V_LIMIT } = types;

const validateComponentTypes = {
  [V_LIMIT]: 'check-length'
}

export default {
    [V_LIMIT]: {
        validate: ({ value, config, type }) => {

			if(!value) return true;
			
			const { max, min } = config;
			let	len = value.toString().length;
			let isValid = true;
      
            if (CHECKED === max.checked) {
              isValid = len <= max.value;
            }
      
            if (CHECKED === min.checked) {
              isValid = isValid && len >= min.value;
            }
      
            return isValid;
          },
          error: ({ config }) => _subTitle(isChecked(config.max.checked, config.max.value), isChecked(config.min.checked, config.min.value))
    }
}

export const getVarifyComponent = (type) => {
    const name = validateComponentTypes[type];

    return require(`../${name}/index`).default;
}